actix-web static files as resources support
Legal
Dual-licensed under MIT
or the UNLICENSE.
Features
- Embed static resources in single self-contained executuble
- Serve static resources in
actix-web
- Install dependencies with npm package manager
- Run custom
npm
run commands (such as webpack) - Support for npm-like package managers (yarn)
- Support for angular-like routers
Usage
Use-case 1: Static resources folder
Create folder with static resources in your project (for example static
):
Add to Cargo.toml
dependencies related to actix-web-static-files
:
[]
= "4.0"
= "4.0"
= "0.2.1"
[]
= "0.2.1"
Add build.rs
with call to bundle resources:
use resource_dir;
Include generated code in src/main.rs
:
use ;
use ResourceFiles;
include!;
async
Run the server:
Request the resource:
> GET
> Host:
> User-Agent:
>
< HTTP/1.1
< content-length:
< content-type:
< etag:
< date:
<
<p>Hello,
) )
See also:
- Static resources folder with index.html example
- Another example with same resources but using own defined function
Use-case 2: package.json - npm managed folder
Create folder with static resources in your project (for example static
):
# install your npm dependencies (here we use fontawesome as an example)
Add generated folder to ignore file of your version control system (here: git):
Add dependencies
and build-dependencies
in Cargo.toml
same way as in the first use-case.
Add build.rs
with call to bundle resources:
use npm_resource_dir;
Include generated code in main.rs
same way as in the first use-case.
Reference resources in your HTML
(static/index.html
):
Hi
Use-case 3: package.json - WebPack usage
Create folder with static resources in your project (for example web
), install required packages and webpack:
# install lodash for usage in example
# install webpack npm dependencies
Add web/webpack.config.js
:
const path = require;
const = require;
const HtmlWebpackPlugin = require;
module.exports = ;
Add web/src/index.js
:
import _ from 'lodash';
document..;
Modify web/package.json
by adding "scripts" sections:
Add to Cargo.toml
dependency to actix-web-static-files
as in the first use case.
Add build.rs
with call to bundle resources:
use NpmBuild;
Include generated code in src/main.rs
:
use ;
use actix_web_static_files;
include!;
async
Run the server:
Request the resource:
> GET
> Host:
> User-Agent:
>
< HTTP/1.1
< content-length:
< content-type:
< etag:
< date:
<
<!DOCTYPE <html>
<head>
<meta charset="UTF-8">
<title>actix-web-static-files </head>
<body>
<script type="text/javascript" src="main.js"></script></body>
) )
See also:
Use-case 4: yarn package manager
We can use another package manager instead of npm
. For example, to use yarn just add .executable("yarn")
to NpmBuild
call:
use NpmBuild;
See also:
Use-case 5: Angular-like applications
If you are using Angular as frontend, you may want to resolve all not found calls via index.html
of frontend app. To do this just call method resolve_not_found_to_root
after resource creation.
use ;
use actix_web_static_files;
use generate;
async
Remember to place you static resource route after all other routes in this case.
You can check the complete example Angular Router Sample.